package com.xiaoma.common.adapter

import android.view.View
import androidx.annotation.IdRes
import androidx.recyclerview.widget.RecyclerView
import com.xiaoma.common.adapter.listener.OnXmItemChildClickListener
import com.xiaoma.common.adapter.listener.OnXmItemClickListener
import com.xiaoma.common.adapter.utils.XmDefaultCallback
import java.util.LinkedHashSet

/**
 * author: liangjingbo
 * date: 2022/6/28
 * describe:
 */
abstract class XmBaseAdapter<T, VH : RecyclerView.ViewHolder>(
    protected val mData: MutableList<T> = mutableListOf()
) : RecyclerView.Adapter<VH>() {

    private var mOnXmItemClickListener: OnXmItemClickListener? = null

    private var mOnXmItemChildClickListener: OnXmItemChildClickListener? = null

    private val childClickViewIds = LinkedHashSet<Int>()

    fun setOnItemClickListener(listener: OnXmItemClickListener?) {
        this.mOnXmItemClickListener = listener
    }

    fun setOnItemChildClickListener(listener: OnXmItemChildClickListener?) {
        this.mOnXmItemChildClickListener = listener
    }


    protected fun addChildClickViewIds(@IdRes vararg viewIds: Int) {
        for (viewId in viewIds) {
            childClickViewIds.add(viewId)
        }
    }

    private fun getChildClickViewIds(): LinkedHashSet<Int> {
        return childClickViewIds
    }


    protected fun bindViewClickListener(holder: VH) {
        mOnXmItemClickListener?.let { listener ->
            holder.itemView.setOnClickListener {
                val position = holder.bindingAdapterPosition
                if (position == RecyclerView.NO_POSITION) {
                    return@setOnClickListener
                }
                listener.onItemClick(position)
            }
        }

        mOnXmItemChildClickListener?.let { listener ->
            for (id in getChildClickViewIds()) {
                holder.itemView.findViewById<View>(id)?.let { childView ->
                    if (!childView.isClickable) {
                        childView.isClickable = true
                    }
                    childView.setOnClickListener {
                        val position = holder.bindingAdapterPosition
                        if (position == RecyclerView.NO_POSITION) {
                            return@setOnClickListener
                        }
                        listener.onItemClick(childView, position)
                    }
                }
            }
        }

    }

    open fun setNewInstance(list: List<T>) {
        this.mData.clear()
        this.mData.addAll(list)
        notifyDataSetChanged()
    }

    open fun setDifferData(list: List<T>) {
        val callback = getDifferCallback() ?: throw
        IllegalStateException("please implement getDifferCallback(): DefaultCallback? !!")
        val oldData = ArrayList(mData)
        mData.clear()
        mData.addAll(list)
        callback.handle(oldData, list).dispatchUpdatesTo(this)
    }

    open fun getDifferCallback(): XmDefaultCallback<T>? {
        return null
    }

    override fun getItemCount(): Int {
        return mData.size
    }

    fun getData() = mData

}